package com.yixing.tech.wcs.tcp;

import com.yixing.tech.wcs.event.DeviceEventMessage;
import com.yixing.tech.wcs.event.DeviceReplyEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class InteractionServiceImpl implements IInteractionService {

    @Resource
    private ApplicationEventPublisher eventPublisher;

    /**
     * message格式 AB660000810001 495051525354495051525354 03
     * */
    @Override
    public void handleState(String message) {
        String state = message.substring(12, 14);
        String s = Integer.toBinaryString(Integer.parseInt(state, 16));
        log.info("分拣台输送线状态:[{}],二进制:[{}]", state, s);
//        int si = Integer.parseInt(s);

        /** 检查是否允许放下料箱 */
        boolean enablePutDown = "100".equals(s) || "1100".equals(s);
        log.info("是否允许放下料箱:{}", enablePutDown);

        /** 检查是否允许取出料箱 */
        boolean enableTakeOut = "1000".equals(s) || "1100".equals(s);
        log.info("是否允许取出料箱:{}", enableTakeOut);

        /** 检查读码头1是否读取到料箱码 */
        StringBuilder podCode1 = new StringBuilder();
        for(int i=14; i<26; i+=2) {
            podCode1.append((char) Integer.parseInt(message.substring(i, i+2), 16));
        }
        log.info("读取到的容器码1[{}]", podCode1.toString());

        /** 检查读码头2是否读取到料箱码 */
        StringBuilder podCode2 = new StringBuilder();
        for(int i=26; i<38; i+=2) {
            podCode2.append((char) Integer.parseInt(message.substring(i, i+2), 16));
        }
        log.info("读取到的容器码2[{}]", podCode2.toString());

        DeviceEventMessage eventMessage = new DeviceEventMessage();
        eventMessage.setEventName("81");
        eventMessage.setPodCode1(podCode1.toString());
        eventMessage.setPodCode2(podCode2.toString());
        eventMessage.setEnablePutDown(enablePutDown);
        eventMessage.setEnableTakeOut(enableTakeOut);

        eventPublisher.publishEvent(new DeviceReplyEvent(eventMessage));
    }

    @Override
    public void reqPutDown() {

    }

    @Override
    public void executePutDown() {

    }

    /** 分拣台输送线回复已收到料箱放下信息 */
    @Override
    public void finPutDown() {
        DeviceEventMessage eventMessage = new DeviceEventMessage();
        eventMessage.setEventName("83");

        eventPublisher.publishEvent(new DeviceReplyEvent(eventMessage));
    }

    /** 分拣台输送线回复已收到料箱处理完成信息 */
    @Override
    public void finPickingTask() {
        DeviceEventMessage eventMessage = new DeviceEventMessage();
        eventMessage.setEventName("84");

        eventPublisher.publishEvent(new DeviceReplyEvent(eventMessage));
    }

    @Override
    public void reqPickUp() {

    }

    /** 分拣台输送线回复已收到取出料箱信息 */
    @Override
    public void finPickUp() {
        DeviceEventMessage eventMessage = new DeviceEventMessage();
        eventMessage.setEventName("86");

        eventPublisher.publishEvent(new DeviceReplyEvent(eventMessage));
    }
}
